package com.youshang520i.demo.mapper;

import com.youshang520i.demo.entity.Stock;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public interface StockMapper {

    @Select("select * from t_stock where id = 1 for update")
//for update
    Stock get();

    @Select("select * from t_stock where id = 1 ")
//for update
    Stock get3();

    @Select("select * from t_stock where id = 1 ")
//for update
    Stock get4();


    @Update("UPDATE t_stock SET version=1+version,stock = stock -1 WHERE id = 1 and (stock - 1) >= 0")
//and stock > 0  and version = #{version}
    int update(int version);

    @Update("UPDATE t_stock SET version=1+version,stock = stock -1 WHERE id = 1 and version = #{version}")
//and stock > 0  and version = #{version}
    int update2(int version);

    @Update("UPDATE t_stock SET stock = stock -1 WHERE id = 1")
//and stock > 0  and version = #{version}
    int add(int version);


}